// let str = 'data: {"id":"as-ym3reni268","object":"chat.completion","created":1713169578,"sentence_id":13,"is_end":false,"is_truncated":false,"result":"。","need_clear_history":false,"finish_reason":"normal","usage":{"prompt_tokens":421,"completion_tokens":259,"total_tokens":680}}data: {"id":"as-ym3reni268","object":"chat.completion","created":1713169578,"sentence_id":13,"is_end":false,"is_truncated":false,"result":"。","need_clear_history":false,"finish_reason":"normal","usage":{"prompt_tokens":421,"completion_tokens":259,"total_tokens":680}}'

function parseMultipleDataStrings(inputString) {
  let res = []
  let stack = []
  let i = 0
  let json = ''
  while (i < inputString.length) {
    if (inputString[i] === '{') {
      stack.push('{')
    } else if (inputString[i] === '}') {
      stack.pop()
      // 如果取干净了
      if (stack.length === 0) {
        json+='}'
        res.push(JSON.parse(json.replaceAll(' ', '')))
        json = ''
      }
    }
    if (stack.length !== 0) {
      json += inputString[i]
    }
    i++
  }
  return res
}

const input =
  'data: {"id":"as-ym3reni268","object":"chat.completion","created":1713169578,"sentence_id":13,"is_end":false,"is_truncated":false,"result":"。","need_clear_history":false,"finish_reason":"normal","usage":{"prompt_tokens":421,"completion_tokens":259,"total_tokens":680}}data: {"id":"as-ym3reni268","object":"chat.completion","created":1713169578,"sentence_id":13,"is_end":false,"is_truncated":false,"result":"。","need_clear_history":false,"finish_reason":"normal","usage":{"prompt_tokens":421,"completion_tokens":259,"total_tokens":680}}'

// console.log(parseMultipleDataStrings(input));

let res = parseMultipleDataStrings(input)
// console.log(res)
console.log(res)
// console.log(JSON.parse(res[1]))

// console.log(
//   JSON.parse(
//     '{"id":"as-ym3reni268","object":"chat.completion","created":1713169578,"sentence_id":13,"is_end":false,"is_truncated":false,"result":"。","need_clear_history":false,"finish_reason":"normal","usage":{"prompt_tokens":421,"completion_tokens":259,"total_tokens":680}'
//   )
// )
